<?php 

require("./PHP/Conn.php");
//require ('./HTML_Parser/simple_html_dom.php');
    
function inputBonito($name, $des,$type,$class)
{
    $str ='<div class="form-group '.$class.'">';
    $str .= '<div class="row-fluid">';
    $str .=     Label($name);
    $str.=  '<input type="'.$type.'"placeholder="'.$des.'" id="id_'.$name.'">';
    $str.='</div></div>'; 
    return $str;
} 

function dropdown($listaValores,$id)
{
    $str='<div class="btn-group">';
    $str.=  '<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">'.$id.
                '<span class="caret"></span></button>';
    $str.=  '<ul class="dropdown-menu" role="menu">';
    $i=0;
    foreach($listaValores as $v)
    {
        $str.='<li style="text-align: center"><a href="#" id="id_a_'.$id.$i++.'">'.implode($v).'&nbsp<span class="glyphicon glyphicon-remove-sign"></span></a></li>'; 
        $str.='<li class="divider"></li>';
    }
        $str.='<li style="text-align: center"><span class="glyphicon glyphicon-plus-sign" style="text-align: center"></span></li>';
    $str.=  '</ul>';
    $str.='</div>'; 
    return $str;
}

function combobox($listaValores,$id)
{
    $str='<div class="form-group">';
    $str.=  '<label for="id_'.$id.'_">'.$id.' de la clave :</label>'; 
    $str.=  '<select class="form-control" id="id_'.$id.'_">'; 
        foreach($listaValores as $v)
            $str.='<option>'.$v.'</option>'; 
    $str.=  ' </select>';
    $str.='</div>';
    return $str;
}  

function combobox0($listaValores,$id)
{
    $str='<div class="form-group">';
    $str.=  '<label for="id_'.$id.'_">'.$id.':</label>'; 
    $str.=  '<select class="form-control" id="id_'.$id.'_">'; 
        foreach($listaValores as $v)
            $str.='<option>'.$v.'</option>'; 
    $str.=  ' </select>';
    $str.='</div>';
    return $str;
} 

function  Probando()
{
    $theads =  Array("NOMBRE","APELLIDO","APELIIDO2");

    $tbodys = array("juan","SALAZAR","CAMBRONERO","leo","SALAZAR","CAMBRONERO","fanny","SALAZAR","CAMBRONERO");

    Tabla($theads, $tbodys,"");
}

function Label($valor)
{
    $str = '<span class="label label-primary">'.$valor.'</span>';
    return $str;
}

function Tabla($theads , $tbodys, $onclick,$id)
{  
    $cantCol=0;
    $cantIns=0;
    
    $str = '<div class="table-responsive"> ';
        $str .= '<table class="table table-striped table-fixed-header" id="'.$id.'">';//  table table-bordered table-fixed-header
            $str.= ' <thead class="header">';

            //lleno thead
            $resultado = count($theads);
             $bool = true;
            foreach($theads as $thead)
            {              
                if($bool)
                {
                    $str.= '<tr>';
                  $bool = false;
                }
                $str.='<th>'.$thead.'</th>';
                $cantCol++;
                if($cantCol == $resultado)
                {
                    $str.='</tr>';
                }
                
            }
            $str.='</thead>';

            //lleno tbody
            $str.='<tbody>';
            foreach ($tbodys as $tbody) {          
                if($cantIns==0)
                {
                    $cantIns++;
                    $str.='<tr onclick="'.$onclick.'" >';      
                    $str.='<td>'.$tbody.'</td>'; 
                                      
                 }elseif( $cantIns != $cantCol && $cantIns <= $cantCol)
                    {
                       $cantIns++;
                       $str.='<td>'.$tbody.'</td>'; 
                           if($cantIns == $cantCol)
                           {
                                $str.='</tr>';  
                                $cantIns = 0;
                           }
                    }                       
            }
            $str.='</tbody>';
        $str.='</table>';
    $str.='</div>';
    //echo ($str);
return  $str;
//$divPrincipal->echo ($str);
}

function crea_usuario()
{  
    $roles = procedure_call("ROLESSISTEMA");
    $roles_ = Array("DEFAULT");
    $privilegios = procedure_call("PRIVSISTEMA"); 
    $privilegios_ = Array("DEFAULT");
    $Temporal= procedure_call("TBSTemporales");
    $Temporales = Array("DEFAULT");
    $tablespaces = procedure_call("TABLESPACES"); 
    $tablespaces_ = Array("DEFAULT");
    $tablas = procedure_call("TODASTABLAS"); 
    $tablas_ = Array();
    foreach($roles as $r)
        array_push($roles_, implode($r));
    foreach($Temporal as $r)
        array_push($Temporales, implode($r));
        foreach($privilegios as $p)
        array_push($privilegios_, implode($p));
    foreach($tablespaces as $t)
        array_push($tablespaces_, implode($t));
    foreach($tablas as $t0)
    { 
        $tv = implode($t0);
        array_push($privilegios_,"SELECT ON ".$tv);
        array_push($privilegios_,"DELETE ON ".$tv);
        array_push($privilegios_,"INSERT ON ".$tv);
        array_push($privilegios_,"UPDATE ON ".$tv);
        array_push($privilegios_,"ALL ON ".$tv);
    }
    $str=  '<div class="form-group">';
    $str.=  '<form role="form">';
    $str.=      '<div class="form-group">'; 
    $str.=          '<h3><strong>Nombre:</strong></h3></br>';
    $str.=          '<div class="form-group abajo">'.inputBonito("Nombre", "Nombre de Usuario", "text","col-md-3").'</div>';
    $str.=          '<div class="row abajo"><h3 class="col-md-12"><strong>Clave:</strong></h3></br>'.selectBox0(Array("DEFAULT","nivel I","nivel II","nivel III","nivel IV"),"seguridadselect","Seguridad de Clave","col-md-3");
    $str.=          inputBonito("Clave", "Inserte una Clave", "password","col-md-3");
    $str.=          inputBonito("Clave1", "Confirme su Clave", "password","col-md-3");
    $str.=          selectBox0(Array("DEFAULT","30 días","60 días","90 días","∞ días"),"caducaselect","Caducidad de Clave","col-md-3")."</div>";
    $str.=          '<div class="row abajo"><h3 class="col-md-12"><strong>Tablespaces:</strong></h3></br>'.selectBox0($tablespaces_,"defaultselect","Tablespace Default","col-md-6");
    $str.=          selectBox0($Temporales,"tempselect","Tablespace Temporal","col-md-6")."</div>";
    $str.=          '<div class="row abajo"><h3 class="col-md-12"><strong>Quotas:</strong></h3></br>'.inputBonito("Quota", "Inserte un Valor", "text","col-md-4");
    $str.=          selectBox0(Array(" ","K","M","G"),"quotaselect","Tamaño de Quota","col-md-4");
    $str.=          '<label class="radio-inline col-md-4"><input type="checkbox" name="inlineRadioOptions" id="quotaunlimited" value="unlimited1"> unlimited</label></div>';
    $str.=          '<div class="row abajo">';
    $str.=              '<div class="form-group col-md-5"><div class="row"><h3 class="col-md-12"><strong>Roles:</strong></h3></br>';
    $str.=                  selectBox0($roles_,"rolselect","Roles Disponibles","col-md-6").'<span class="glyphicon glyphicon-plus-sign col-md-1" onclick="addNuevaRow('."'rolselect'".')"></span></div>';
    $str.=                  Tabla(Array("Row","Role"),Array(),"","tablarol");
    $str.=              '</div>';
    $str.=              '<div class="form-group col-md-7"><div class="row"><h3 class="col-md-12"><strong>Privilegios:</strong></h3></br>';
    $str.=                  selectBox0($privilegios_,"privselect","Privilegios Disponibles","col-md-6").'<span class="glyphicon glyphicon-plus-sign col-md-1" onclick="addNuevaRow('."'privselect'".')"></span></div>';
    $str.=                  Tabla(Array("Row","Privilege"),Array(),"","tablapriv");
    $str.=          '</div></div>';
    $str.=      '</div>';
    $str.=  '</form> ';   
    $str.= "'tablarol'";
    $str.=  '<button class="btn btn-default" onclick="creaUser()">Agregar</button>';
    $str.=  '</div>';
    echo $str;
}

function selectBox($opciones,$id,$labelName)
{
    $str ='<div class="form-group">';
    $str .= '<div class="row-fluid">';
    $str .=     Label($labelName);
    $str .=     '<select class="selectpicker" id="'.$id.'" data-style="btn-primary">';
            foreach ($opciones as $value) 
                $str .= '<option>'.$value.'</option>';          
        $str.=  '</select>';
    $str .= '</div></div>';
    return $str; 
} 

function selectBox0($opciones,$id,$labelName,$class)
{
    $str ='<div class="form-group '.$class.'">';
    $str .= '<div class="row-fluid">';
    $str .=     Label($labelName);
    $str .=     '<select class="selectpicker" id="'.$id.'" data-style="btn-primary">';
            foreach ($opciones as $value) 
                $str .= '<option>'.$value.'</option>';          
        $str.=  '</select>';
    $str .= '</div></div>';
    return $str; 
}

function Label_Input($opciones,$idtext,$labelName,$type)
{
     $str  = '<div class="row-fluid">';
        $str .= Label($labelName);
        foreach ($opciones as $value) {
            $str.=  '<input type="'.$type.'" class="form-control" placeholder="'.$value.'" id="id_'.$idtext.'">';
        }   
     $str .= '</div>';
    
    return $str;
    
}

function label_checkbox($checkboxes,$nomCheck, $idCheck,$labelName)
{
    $str  = '<div class="row-fluid">';
        $str .= Label($labelName);
        //foreach ($checkboxes as $value) {
            $str.= '<div class="checkbox">';
                      $str .= '<label><input id="'.$idCheck.'" type="checkbox" value="Activar">Activar</label>';
                      $str .= '<label><input id="'.$idCheck.'" type="checkbox" value="Desactivar">Desactivar</label>';
                $str .='   </div>';
     //   }   
     $str .= '</div>';
    
    return $str;
}
